package com.xianbeiying.app.ckgl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.LogOutBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.PhoneUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;

/**
 * 找回密码
 */
public class ForgotPasswordActivity extends BaseActivity implements View.OnClickListener, HttpCallBackUtil {
    private ImageView iv_title_left;
    private TextView tv_title;
    private EditText et_user_name, et_phone, et_code;
    private Button change_btn, code_btn;
    private SetModel setModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);
        initView();
    }

    private void initView() {
        setModel=new SetModel(this);
        setModel.addResponseListener(this);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        et_user_name = (EditText) findViewById(R.id.et_user_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_code = (EditText) findViewById(R.id.et_code);
        change_btn = (Button) findViewById(R.id.change_btn);
        code_btn = (Button) findViewById(R.id.code_btn);
        tv_title.setText("忘记密码");
        iv_title_left.setOnClickListener(this);
        change_btn.setOnClickListener(this);
        code_btn.setOnClickListener(this);

    }

    private LogOutBean logOutBean1;
    private LogOutBean logOutBean2;
    @Override
    public void onCallBackData(String url, Object content) {

        if(url.equals("获取验证码")){
            logOutBean1= JSON.parseObject(new String((byte[]) content), LogOutBean.class);
            if(logOutBean1.isR()){
                ToastUtil.showInfo(ForgotPasswordActivity.this,"发送成功");
            }else{
                ToastUtil.showInfo(ForgotPasswordActivity.this,"发送失败");
            }
        }

        if(url.equals("验证")){

            logOutBean2= JSON.parseObject(new String((byte[]) content), LogOutBean.class);
            if(logOutBean2.isR()){
                ToastUtil.showInfo(ForgotPasswordActivity.this,"验证成功");
                startActivity(new Intent(ForgotPasswordActivity.this,ResetPasswordActivity.class).putExtra("username",userName));
                 finish();
            }else{

                ToastUtil.showInfo(ForgotPasswordActivity.this,"验证失败");
            }

        }

    }
    String userName;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.change_btn:
                userName = et_user_name.getText().toString().trim();
                String phone = et_phone.getText().toString().trim();
                String code = et_code.getText().toString().trim();
                if(userName.isEmpty()){
                    ToastUtil.showInfo(this,"登录名不能为空");
                    return;
                }
                if(phone.isEmpty()){
                    ToastUtil.showInfo(this,"手机号不能为空");
                    return;
                }
                if(code.isEmpty()){
                    ToastUtil.showInfo(this,"效验码不能为空");
                    return;
                }
                setModel.check_verification_code(userName,code,true);
                break;
            //获取验证码
            case R.id.code_btn:
                String userNames = et_user_name.getText().toString().trim();
                String phones= et_phone.getText().toString().trim();
                if(userNames.isEmpty()){
                    ToastUtil.showInfo(this,"登录名不能为空");
                    return;
                }
                if(phones.isEmpty()){
                    ToastUtil.showInfo(this,"手机号不能为空");
                    return;
                }
                if (!PhoneUtil.isPhone(phones)){
                    ToastUtil.showInfo(this,"手机号格式不正确");
                    return;
                }
                //开始倒计时
                startCountDownTime(60);
                setModel.get_verification_code(userNames,phones,true);
                break;
        }
    }

    /**
     * 倒计时
     *
     * @param time 秒数
     */
    private void startCountDownTime(final long time) {
        CountDownTimer timer = new CountDownTimer(time * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished){
                code_btn.setOnClickListener(null);
                code_btn.setText(millisUntilFinished / 1000 + "秒");
            }
            @Override
            public void onFinish() {
                code_btn.setOnClickListener(ForgotPasswordActivity.this);
                code_btn.setText("获取验证码");
            }
        }.start();
    }
}
